import Vue from 'vue';
import Router from 'vue-router';

import Homepage from '@/phr/ui/home/Homepage.vue';
import MoreNews from '@/phr/ui/home/MoreNews.vue';
import NewsDetail from '@/phr/ui/home/NewsDetail.vue';
import Login from '@/phr/ui/auth/Login.vue';
import Login2 from '@/phr/ui/auth/Login2.vue';
import Register from '@/phr/ui/auth/Register.vue';
import Registration from '@/phr/ui/registration/Registration.vue';
import Registration2 from '@/phr/ui/registration/Registration2.vue';
import MedicalRecord from '@/phr/ui/medicalRecord/MedicalRecord.vue';
import MedicineInformation from '@/phr/ui/medicine/MedicineInformation.vue';
import Map from '@/phr/ui/map/Map.vue';
import HealthIndex from '@/phr/ui/healthIndex/HealthIndex.vue';
import Test from '@/phr/myTest/views/Test.vue';
import TestApi from '@/phr/myTest/views/TestApi.vue';
import {RawLocation, Route} from 'vue-router/types/router';
import authService from '@/phr/service/auth/AuthService'
import {SUCCESS} from '@/phr/common/ErrorCode';
import store from '@/store'

Vue.use(Router);

export default new Router({
  // mode:'history',
  routes: [
    {
      path: '/',
      name: 'Index',
      component: Homepage
    },
    {
      path: '/homepage',
      name: 'Homepage',
      component: Homepage
    },
    {
      path: '/news',
      name: 'NewsDetail',
      component: NewsDetail
    },
    {
      path: '/moreNews',
      name: 'MoreNews',
      component: MoreNews
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/login2',
      name: 'Login2',
      component: Login2
    },
    {
      path: '/register',
      name: 'Register',
      component: Register
    },
    {
      path: '/registration',
      name: 'Registration',
      component: Registration,
      beforeEnter: (to, from, next) => checkLogin(to, from, next)
    },
    {
      path: '/registration2',
      name: 'Registration2',
      component: Registration2
    },
    {
      path: '/medicalRecord',
      name: 'MedicalRecord',
      component: MedicalRecord,
      beforeEnter: (to, from, next) => checkLogin(to, from, next)
    },
    {
      path: '/medicine',
      name: 'MedicineInformation',
      component: MedicineInformation
    },
    {
      path: '/map',
      name: 'Map',
      component: Map
    },
    {
      path: '/healthIndex',
      name: 'HealthIndex',
      component: HealthIndex,
      beforeEnter: (to, from, next) => checkLogin(to, from, next)

    },
    {
      path: '/test',
      name: 'Test',
      component: Test
    },
    {
      path: '/testApi',
      name: 'TestApi',
      component: TestApi,
    }
  ]
});

/**
 * 一些需要登录后才能用的功能界面
 * 会检验当前token是否有效，如果无效就跳转到登录界面，否则跳转回对应的界面
 * @param {Route} to
 * @param {Route} from
 * @param next
 */
function checkLogin(to: Route, from: Route, next) {
  authService.validateToken()
      .then(res => {
        //检查TOKEN是否是合法的，如果合法跳到对应的界面
        if (res.code == SUCCESS && res.value) {
          next()
        } else {
          //缓存当前界面的PATH
          store.commit('setTargetPath', to.path);
          //跳到登录界面
          next('/login2')
        }
      })
      .catch(e => {
        console.log(e);
        next('/login2')
      })
}